package com.alpsoft.spotcheck.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.alpsoft.spotcheck.model.User;

/**
 * 登录信息缓存的类
 * 
 * @author Nasir
 * 
 */
public class SharedPreferencesUtils {

	private static final String FILE_NAME = "loginUser";// 文件名称
	private static final String EMP_ID = "empId";// 登录员工的ID
	private static final String EMP_PASSWORD = "emppassword"; // 员工的密码
	private static final String EMP_NAME = "empName";// 登录员工的ID
	private static final String EMP_DEPTID = "deptId";// 登录员工的部门ID
	private static final String SESSION_ID = "sessionId";// 登录员工的ID
	private static final String EMP_ICCODE = "icCode";
	private static final String EMP_PERNAME = "personName";

	/**
	 * 
	 * @date 2015-5-6
	 * @description 将登录人员sessionId存储到本地SharedPreferences中
	 * @param pContext
	 * @param sessionId
	 */
	public static void setSessionId(Context pContext, String sessionId) {
		SharedPreferences pfs = pContext.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		Editor editor = pfs.edit();
		editor.putString(SESSION_ID, sessionId);
		editor.commit();
	}

	/**
	 * 
	 * @date 2015-5-6
	 * @description 得到存储在本地SharedPreferences中的登录人员sessionId
	 * @param pContext
	 * @param sessionId
	 */
	public static String getSessionId(Context pContext) {
		SharedPreferences pfs = pContext.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		String sessionId = pfs.getString(SESSION_ID, "");
		return sessionId;
	}

	/**
	 * 
	 * @date 2015-5-4
	 * @description 将登录人员信息存储到本地SharedPreferences中
	 * @param pContext
	 * @param user
	 */
	public static void setLoginInfo(Context pContext, User user) {
		SharedPreferences pfs = pContext.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		Editor editor = pfs.edit();
		editor.putString(EMP_ID, user.getUserId());
		editor.putString(EMP_NAME, user.getUserName());
		editor.putString(EMP_DEPTID, user.getDeptId());
		editor.putString(EMP_ICCODE, user.getIcCode());
		editor.putString(EMP_PERNAME, user.getPersonName());
		editor.putString(EMP_PASSWORD, user.getPassword());
		editor.commit();
	}

	/**
	 * 
	 * @date 2015-5-4
	 * @description 得到存储在本地SharedPreferences中的登录人员信息
	 * @param pContext
	 * @return
	 */
	public static User getLoginInfo(Context pContext) {
		SharedPreferences pfs = pContext.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		String empId = null;
		String empName = null;
		String deptId = null;
		String icCode = null;
		String personName = null;
		String password = null;
		if (pfs != null) {
			empId = pfs.getString(EMP_ID, "");
			empName = pfs.getString(EMP_NAME, "");
			deptId = pfs.getString(EMP_DEPTID, "");
			icCode = pfs.getString(EMP_ICCODE, "");
			personName = pfs.getString(EMP_PERNAME, "");
			password = pfs.getString(EMP_PASSWORD, "");
		}
		User loginUser = new User();
		System.out.println("empId:" + empId + "empName:" + empName + "deptId:"
				+ deptId + "icCode:" + icCode + "personName:" + personName);
		loginUser.setUserId(empId);
		loginUser.setPassword(password);
		loginUser.setUserName(empName);
		loginUser.setDeptId(deptId);
		loginUser.setIcCode(icCode);
		loginUser.setPersonName(personName);
		return loginUser;
	}

	public static String getEmpName(Context pContext) {
		SharedPreferences pfs = pContext.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		String empName = null;
		if (pfs != null) {
			empName = pfs.getString(empName, "");
		}
		return empName;

	}
}
